{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python37764bitd2lconda94fc7ab78ae34cabbef0e75f5636f253",
   "display_name": "Python 3.7.7 64-bit ('d2l': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "tensor([[ 0,  1,  2,  3],\n        [ 4,  5,  6,  7],\n        [ 8,  9, 10, 11],\n        [12, 13, 14, 15]])"
     },
     "metadata": {},
     "execution_count": 6
    }
   ],
   "source": [
    "import torch\n",
    "x = torch.arange(16).reshape(4,4)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Help on built-in function view:\n\nview(...) method of torch.Tensor instance\n    view(*shape) -> Tensor\n    \n    Returns a new tensor with the same data as the :attr:`self` tensor but of a\n    different :attr:`shape`.\n    \n    The returned tensor shares the same data and must have the same number\n    of elements, but may have a different size. For a tensor to be viewed, the new\n    view size must be compatible with its original size and stride, i.e., each new\n    view dimension must either be a subspace of an original dimension, or only span\n    across original dimensions :math:`d, d+1, \\dots, d+k` that satisfy the following\n    contiguity-like condition that :math:`\\forall i = d, \\dots, d+k-1`,\n    \n    .. math::\n    \n      \\text{stride}[i] = \\text{stride}[i+1] \\times \\text{size}[i+1]\n    \n    Otherwise, it will not be possible to view :attr:`self` tensor as :attr:`shape`\n    without copying it (e.g., via :meth:`contiguous`). When it is unclear whether a\n    :meth:`view` can be performed, it is advisable to use :meth:`reshape`, which\n    returns a view if the shapes are compatible, and copies (equivalent to calling\n    :meth:`contiguous`) otherwise.\n    \n    Args:\n        shape (torch.Size or int...): the desired size\n    \n    Example::\n    \n        >>> x = torch.randn(4, 4)\n        >>> x.size()\n        torch.Size([4, 4])\n        >>> y = x.view(16)\n        >>> y.size()\n        torch.Size([16])\n        >>> z = x.view(-1, 8)  # the size -1 is inferred from other dimensions\n        >>> z.size()\n        torch.Size([2, 8])\n    \n        >>> a = torch.randn(1, 2, 3, 4)\n        >>> a.size()\n        torch.Size([1, 2, 3, 4])\n        >>> b = a.transpose(1, 2)  # Swaps 2nd and 3rd dimension\n        >>> b.size()\n        torch.Size([1, 3, 2, 4])\n        >>> c = a.view(1, 3, 2, 4)  # Does not change tensor layout in memory\n        >>> c.size()\n        torch.Size([1, 3, 2, 4])\n        >>> torch.equal(b, c)\n        False\n\n"
    }
   ],
   "source": [
    "help(x.view)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "torch.Size([4, 4])"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "x.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "torch.Size([16])"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "y = x.view(16)\n",
    "y.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "torch.Size([2, 8])"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "z = x.view(-1, 8)  # the size -1 is inferred from other dimensions\n",
    "z.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "tensor([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15])"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "a = x.view(-1)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "torch.Size([16])"
     },
     "metadata": {},
     "execution_count": 14
    }
   ],
   "source": [
    "a.size()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}